package edu.ahpu.propertymanage.controller;

import edu.ahpu.propertymanage.dataclass.UserLogin;
import edu.ahpu.propertymanage.pojo.Managers;
import edu.ahpu.propertymanage.pojo.Owner;
import edu.ahpu.propertymanage.pojo.Worker;
import edu.ahpu.propertymanage.service.ManagersService;
import edu.ahpu.propertymanage.service.OwnerService;
import edu.ahpu.propertymanage.service.PropertyTageService;
import edu.ahpu.propertymanage.service.WorkerService;
import edu.ahpu.propertymanage.util.GetID;
import edu.ahpu.propertymanage.util.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/forget")
public class ForgetPwController {

    @Autowired
    ManagersService managersService;

    @Autowired
    WorkerService workerService;

    @Autowired
    OwnerService ownerService;

    @Autowired
    GetID getID;

    @Autowired
    SendSms sendSms;

    @Autowired
    PropertyTageService propertyTageService;

    String codeString = null;


    @PostMapping("checkid")
    public String checkID(String id) {
        //获取tage标记
        int tageLength = propertyTageService.getById(1).getTage().length();
        String tage = id.substring(tageLength + 1, tageLength + 2);
        //获取用户列表
        switch (tage) {
            case "O":
                List<Owner> listO = ownerService.list();
                for (Owner o : listO) {
                    if (o.getOwnerId().equals(id))
                        return "checkRight";
                }
                break;
            case "M":
                List<Managers> listM = managersService.list();
                for (Managers m : listM) {
                    if (m.getManagerId().equals(id))
                        return "checkRight";
                }
                break;
            case "W":
                List<Worker> listW = workerService.list();
                for (Worker w : listW) {
                    if (w.getWorkerId().equals(id))
                        return "checkRight";
                }
                break;
            default:
                return "error";
        }
        return "error";
    }

    @PostMapping("getphone")
    public String getPhone(String forgetid) {
        //获取tage标记
        int tageLength = propertyTageService.getById(1).getTage().length();
        String tage = forgetid.substring(tageLength + 1, tageLength + 2);

        String phone = "";
        //获取用户列表
        switch (tage) {
            case "O":
                Owner owner = ownerService.getById(forgetid);
                phone = owner.getOwnerPhone();
                break;
            case "M":
                Managers managers = managersService.getById(forgetid);
                phone = managers.getManagerPhone();
                break;
            case "W":
                Worker worker = workerService.getById(forgetid);
                phone = worker.getWorkerPhone();
                break;
            default:
                return "error";
        }

        return phone.substring(0, 3) + "******" + phone.substring(9, 11);
    }

    @PostMapping("getsms")
    public String getSms(String forgetid) {

        String phoneName = "";
        Managers managers = managersService.getById(forgetid);
        if (managers != null) {
            phoneName = managers.getManagerPhone();
        }
        Worker worker = workerService.getById(forgetid);
        if (worker != null) {
            phoneName = worker.getWorkerPhone();
        }
        Owner owner = ownerService.getById(forgetid);
        if (owner != null) {
            phoneName = owner.getOwnerPhone();
        }


        String code = getID.getSms();
        System.out.println("*****************");
        System.out.println(code);
        System.out.println("*****************");
        if ("OK".equals(sendSms.sendSms(phoneName, code))) {
//        if (true) {//取消调用sendSms
            codeString = code;
//            request.getSession().setAttribute("code",code);
            return "success-sms";
        } else
            return "error";
    }

    @PostMapping("checksms")
    public String checkSms(String code) {
        if (code.equals(codeString)) {
            codeString = null;
            return "success-check";
        } else
            return "error";
    }

    @PostMapping("resetpw")
    public String resetPw(@RequestBody UserLogin userLogin) {
        boolean bool = false;
        Owner owner = ownerService.getById(userLogin.getUname());
        if (owner != null) {
            owner.setOwnerPw(userLogin.getPassword());
            bool = ownerService.updateById(owner);
        }
        Worker worker = workerService.getById(userLogin.getUname());
        if (worker!=null){
            worker.setWorkerPw(userLogin.getPassword());
            bool=workerService.updateById(worker);
        }
        Managers managers = managersService.getById(userLogin.getUname());
        if (managers!=null){
            managers.setManagerPw(userLogin.getPassword());
            bool=managersService.updateById(managers);
        }
        if (bool)//取消存入数据库中
            return "success-reset";
        else
            return "error";
    }

}
